package com.kungfuhacking.wristbandpro.location.activity;

import android.app.ProgressDialog;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.kungfuhacking.wristbandpro.R;
import com.kungfuhacking.wristbandpro.base.view.BaseActivity;
import com.kungfuhacking.wristbandpro.custom.TitleBarView;
import com.kungfuhacking.wristbandpro.location.presenter.ISharePresenter;
import com.kungfuhacking.wristbandpro.location.view.IShareView;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.friends.Wechat;

/**
 * 微信分享activity
 * Created by Aaron on 17/5/9.
 */

public class ShareActivity extends BaseActivity implements IShareView, View.OnClickListener {

    private ISharePresenter iSharePresenter = new ISharePresenter(this);
    private com.kungfuhacking.wristbandpro.custom.TitleBarView tbv;
    private android.widget.TextView tvinvite;
    private android.widget.TextView tvwechatinvite;
    private ProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share);
        ShareSDK.initSDK(this);//初始化sharesdk
        initWidget();

        iSharePresenter.getInviteKey();
    }

    private void initWidget() {
        this.tvwechatinvite = (TextView) findViewById(R.id.tv_wechat_invite);
        this.tvinvite = (TextView) findViewById(R.id.tv_invite);
        this.tbv = (TitleBarView) findViewById(R.id.tbv);
        tbv.setOnLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvwechatinvite.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        iSharePresenter.clearRequest();
    }

    @Override
    public String getShareCode() {
        return tvinvite.getText().toString();
    }

    @Override
    public void setShareCode(String code) {
        tvinvite.setText(code);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.tv_wechat_invite://邀请 弹框
                if (!TextUtils.isEmpty(tvinvite.getText().toString()))
                    showShareDialog();
                else
                    showErr("您的权限不够,无法邀请");
                break;

            case R.id.tv_wechat_friend://邀请
                if (dialog != null && dialog.isShowing())
                    dialog.dismiss();

                iSharePresenter.share(Wechat.NAME, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
                break;
        }
    }

    /**
     * 弹出分享的对话框
     * //     * @param content 分享的内容
     * //     * @param url     分享的Url
     */
    private void showShareDialog() {
        dialog = new ProgressDialog(this);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
        dialog.setContentView(R.layout.layout_share_friend);
//        TextView tv_wechat_circle = (TextView) dialog.findViewById(R.id.tv_wechat_circle);
        TextView tv_wechat_friend = (TextView) dialog.findViewById(R.id.tv_wechat_friend);
        //微信好友
        tv_wechat_friend.setOnClickListener(this);

    }
}